var app = getApp()
var baseUrl = app.globalData.baseUrl
Page({
  data: {
    goods: [],
    auctionStatus: true,
    callStatus: false
  },

  onLoad(options) {},
  onShow() {
    let user = wx.getStorageSync('userInfo')
    let publishid = user.id
    this.getPublishGoods(publishid)
  },

  //查找自己发布的商品
  async getPublishGoods(publisherid) {
    wx.request({
      url: baseUrl+'goods/myGoods/'+publisherid,
      method: 'GET',
      success: res => {
        this.setData({
          goods: res.data
        })
      }
    })
  },

  toGoods(e) {
    let goodsid = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/goods/goods?goodsid=' + goodsid,
    })
  },

  doPublish() {
    wx.navigateTo({
      url: '/pages/doPublish/doPublish',
    })
  },

  isauction() {
    this.setData({
      auctionStatus: true
    })
  },
  noauction() {
    this.setData({
      auctionStatus: false
    })
  },
  callStatus(e) {
    let openId = e.currentTarget.dataset.buyer
    if (!openId) {
      wx.showToast({
        title: '哦豁，没人买哦',
        icon: 'error'
      })
    } else {
      //获取卖家联系方式
      wx.request({
        url: baseUrl+'address/getAddrByOpenid/'+openId,
        method:'GET',
        success: res => {
          console.log(res)
          this.setData({
            address: res.data
          })
        }
      })
      this.setData({
        callStatus: true
      })
    }
  },
  closeCall() {
    this.setData({
      callStatus: false
    })
  },
  copyPhone() {
    wx.setClipboardData({
      data: this.data.address[0].phone,
      success: (res) => {
        wx.showToast({
          title: '复制成功',
        })
      }
    })
  },
  copyQQ() {
    wx.setClipboardData({
      data: this.data.address[0].qq,
      success: (res) => {
        wx.showToast({
          title: '复制成功',
        })
      }
    })
  },
  //删除物品
  delete(e) {
    let delId = e.currentTarget.dataset.delid
    //删除之前查询是否有人参与了竞拍，有竞拍记录时不允许删除
      wx.request({
        url: baseUrl+'goodsAuctionRecord/get/'+delId,
        method:'GET',
        success: (res) => {
          if (res.data.length > 0) {
            wx.showToast({
              title: '不允许删除有出价记录的物品',
              icon: 'none'
            })
          } else {
            //允许删除
            wx.showModal({
              title: '删除商品',
              content: '是否要删除该物品？',
              success: (res) => {
                if (res.confirm) {
                  wx.showLoading({
                    title: '正在删除'
                  })
  
                  wx.request({
                    url: baseUrl+'goods/delete/'+delId,
                    method: 'POST'
                  })
                }
              }
            })
          }
        }
      })
  }
})